package com.example.whm;



import java.util.HashMap;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.Application;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.app.ProgressDialog;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.Toast;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.example.whm.model.User;

public class FiveFragment extends Fragment implements OnClickListener{

	private AQuery aq;
	private AQuery aq2;
	
	private String topic;
	 SharedPreferences sp;
	
	
	
	

    @Override
 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
 			Bundle savedInstanceState) {
     	View view = inflater.inflate(R.layout.five_layout2, container, false);  
     	 sp=((Application)getActivity().getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
     	
     	 
     	aq=new AQuery(getActivity());
    	aq2=new AQuery(getActivity(), view);
    	aq2.id(R.id.five_layoutline_buy_record).clicked(FiveFragment.this);
    	aq2.id(R.id.five_layoutline_myaddress).clicked(FiveFragment.this);
    	aq2.id(R.id.five_layoutline_flow).clicked(FiveFragment.this);
    	aq2.id(R.id.five_layout_loginout).clicked(FiveFragment.this);
     	return view;
 	}




    
    
    
    
    

    @Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		 if (sp.getString("user", null)!=null) {
				User u=JSONObject.parseObject(sp.getString("user", null), User.class);
				System.out.println("sp get user="+sp.getString("user", null));
				aq2.id(R.id.five_layout_name).text(u.getName());
				aq2.id(R.id.five_layout_area).text(u.getAdd()==null?"":(u.getAdd().getProvince()+"-"+u.getAdd().getCity()+"-"+u.getAdd().getArea()));
				aq2.id(R.id.five_layout_phone).text(u.getPhone());
			}
		
	}










	MainActivity m;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		 m=(MainActivity) activity;
		
	}











	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) { 
		  case R.id.five_layoutline_buy_record:
		    	startActivity(new Intent(getActivity(), BuyRecordActivity.class));
		    	break;
		  case R.id.five_layoutline_myaddress:
			  startActivity(new Intent(getActivity(), MyAddressDetail.class));
			  break;
		  case R.id.five_layoutline_flow:
			  startActivity(new Intent(getActivity(), FlowList.class));
			  break;
		  case R.id.five_layout_loginout:
			  login_out();
			  break;
		 default:  
		        break;  
		}
	}
    
	ProgressDialog dialog;
	public void show_dialog(boolean b){
		
	
		if (dialog==null) {
			dialog = new ProgressDialog(getSupportActivity());
		    dialog.setIndeterminate(true);
		    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		    dialog.setMessage("login out");
		}
		if (b) {
			dialog.show();
		}else{
			dialog.dismiss();
		} 
	    
	}
	
	public void login_out(){
		 show_dialog(true);
		  String url="http://"+Whm_app.api_url+":9000/quit";
		  AjaxCallback<String> cb = new AjaxCallback<String>();        
		  cb.url(url).type(String.class).weakHandler(this, "ui");
		 
	  	String cookiename=sp.getString("cookie_name",null);
	  	String cookievalue=sp.getString("cookie_value",null);
	  	if (cookiename!=null&&cookievalue!=null) {
	  		cb.cookie(cookiename,cookievalue);               
		}
		//cb.params(new HashMap<String, String>());
		cb.method(1);
		  aq.ajax(cb);
	  }
	
	 public void ui(String url, String r, AjaxStatus status){
		 show_dialog(false);	 
		 Toast.makeText(getActivity(), "r="+r+"status.getCode()="+status.getCode(), 1000).show();
		  if (status.getCode()==200) {
			  SharedPreferences sp=((Application)getActivity().getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
			  sp.edit().clear().commit();
			  //sp.getAll().clear();
			  m.getAq().find(R.id.main_bott_one).click();
			  
		  }
		  
	 
	 
	 }
			 
			  
	
	
	
 
}
